package com.ruoyi.bus.controller.front;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.bus.config.FrontConfig;
import com.ruoyi.bus.domain.CustAddr;
import com.ruoyi.bus.domain.CustBase;
import com.ruoyi.bus.service.ICustBaseService;
import com.ruoyi.bus.service.NoGenerateServlce;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.http.HttpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api")
public class FrontLoginController {

    @Autowired
    private ICustBaseService custBaseService;
    @Autowired
    private NoGenerateServlce noGenerateServlce;

    @RequestMapping("/login")
    @ResponseBody
    public Map<String,String> getOpenId(String code){
        Map<String,String> res=new HashMap<>();
        String result="";
        try{//请求微信服务器，用code换取openid。HttpUtil是工具类，后面会给出实现，Configure类是小程序配置信息，后面会给出代码
            result = HttpUtils.sendGet(
                    "https://api.weixin.qq.com/sns/jscode2session?appid="
                            + FrontConfig.mini_appID + "&secret="
                            + FrontConfig.mini_secret + "&js_code="
                            + code
                            + "&grant_type=authorization_code", null);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(result);
        JSONObject jsonObj = JSONObject.parseObject(result);//解析从微信服务器上获取到的json字符串
        System.out.println("用户的openid为："+jsonObj.get("openid"));//此处也可以得到session_key的值
        res.put("session_key",jsonObj.get("session_key").toString());

        CustBase custUser=custBaseService.getOne(new QueryWrapper<>(new CustBase().setOpenId(jsonObj.get("openid").toString())));//去数据库判断用户是否存在该用户
        if(custUser!=null)//如果存在该用户
        {
            res.put("openId",custUser.getOpenId());//将用户openId返回
            res.put("customerId",custUser.getCustomerId());//将用户id返回
            res.put("customerTele",custUser.getCustomerTele());//将用户的手机号返回
            return res;
        }
        //如果是新用户，就添加用户到数据库中
        CustBase frontUserInfo=new CustBase();//封装小程序对象
        frontUserInfo.setCustomerId(noGenerateServlce.getSeqNo("CU",6));
        frontUserInfo.setOpenId(jsonObj.get("openid").toString());
        //将用户信息保存到数据库中
        custBaseService.save(frontUserInfo);
        res.put("openId",frontUserInfo.getOpenId());
        res.put("customerId",frontUserInfo.getCustomerId());
        return res;
    }

    @RequestMapping("/updateUserInfo")
    @ResponseBody
    public boolean updateUserInfo(@RequestBody CustBase custBase){
        CustBase custUser=custBaseService.getOne(new QueryWrapper<>(new CustBase().setCustomerId(custBase.getCustomerId())));//去数据库判断用户是否存在该用户
        custUser.setNickName(custBase.getNickName()).setGender(custBase.getGender())
                .setAvatarUrl(custBase.getAvatarUrl())
                .setCustomerName(custBase.getNickName())
                .setProvince(custBase.getProvince()).setCity(custBase.getCity());
        custBaseService.updateById(custUser);
        return true;
    }


    @RequestMapping("/setTele")
    @ResponseBody
    public AjaxResult updateUserInfo(String customerId,String customerTele){
        CustBase custUser=custBaseService.getOne(new QueryWrapper<>(new CustBase().setCustomerId(customerId)));//去数据库判断用户是否存在该用户
        custUser.setCustomerTele(customerTele);
        custBaseService.updateById(custUser);
        return AjaxResult.success("保存成功");
    }



}
